package DAO;

import Connection.ConnectionFactory;
import Interfaces.Inter_CodVal;
import Model.CodVal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class DAO_CodVal implements Inter_CodVal{
     
    Connection con;
     
     public DAO_CodVal() throws Exception{
        try{
            this.con =ConnectionFactory.getConnection();
        }
        catch(Exception e){
            throw new Exception("Erro ao conectar no Codigo de validação\n"+e.getMessage());
        }
    }
     
      public void GravaCod(CodVal codv) throws Exception{
        PreparedStatement ps;
        ResultSet rs;
        double cv =0.0, i;
        
        do{
            for(i=0;i<8;i++){
                cv=Math.random()*i;
            }
        }while(cv==codv.getCodval());
        
        codv.setCodval(cv);
        
        if(codv ==null){
            throw new Exception("Nao pode gravar um usuário nulo");
        }
        try{
            String sql ="INSERT INTO CodVal(idCodval, codval) values(?,?)";
            ps =con.prepareStatement(sql);
            ps.setInt(1,codv.getIdCodval());            
            ps.setDouble(2,codv.getCodval());
            
            ps.executeUpdate();
        }
         catch(SQLException e)
        {
            throw new Exception("Erro ao inserir Codigo Validação \n"+e.getMessage());
        }
    }
    
};

